﻿#include "RankingDialog.h"
#include "../../MirGraphics/MLibrary.h"
#include "../../Settings.h"
#include "../../MirSounds/SoundManager.h"
#include "../../Forms/Client.CMain.h"
#include "../../../Shared/ClientPackets.h"
#include "../../MirNetwork/Network.h"
#include "../GameScene.h"
#include "MainDialogs.h"

using namespace Client::MirControls;
using namespace Client::MirGraphics;
using namespace Client::MirSounds;

namespace Client::MirScenes::Dialogs
{

    RankingDialog::RankingDialog()
    {
        setIndex(728);
        setLibrary(Libraries::Title);
        //Size = new Size(288, 324);
        setMovable(true);
        setSort(true);
        Point tempVar((Settings::ScreenWidth - getSize()->Width) / 2, (Settings::ScreenHeight - getSize()->Height) / 2);
        setLocation(&tempVar);

        CloseButton = new MirButton();
        CloseButton->setHoverIndex(361);
        CloseButton->setIndex(360);
        Point tempVar2(300, 3);
        CloseButton->setLocation(&tempVar2);
        CloseButton->setLibrary(Libraries::Prguse2);
        CloseButton->setParent(this);
        CloseButton->setPressedIndex(362);
        CloseButton->setSound(SoundList::ButtonA);
        CloseButton->Click += [&] (o, e)
        {
            Hide();
        };

        AllButton = new MirButton();
        AllButton->setIndex(751);
        AllButton->setPressedIndex(752);
        AllButton->setHoverIndex(753);
        AllButton->setLibrary(Libraries::Title);
        AllButton->setHint("Overall TOP 20");
        Point tempVar3(10, 38);
        AllButton->setLocation(&tempVar3);
        AllButton->setParent(this);
        AllButton->setSound(SoundList::ButtonA);
        AllButton->Click += [&] (o, e)
        {
            RequestRanks(0);
        };
        TaoButton = new MirButton();
        TaoButton->setIndex(760);
        TaoButton->setPressedIndex(761);
        TaoButton->setHoverIndex(762);
        TaoButton->setLibrary(Libraries::Title);
        TaoButton->setHint("TOP 20 Taoists");
        Point tempVar4(40, 38);
        TaoButton->setLocation(&tempVar4);
        TaoButton->setParent(this);
        TaoButton->setSound(SoundList::ButtonA);
        TaoButton->Click += [&] (o, e)
        {
            RequestRanks(3);
        };
        WarButton = new MirButton();
        WarButton->setIndex(754);
        WarButton->setPressedIndex(755);
        WarButton->setHoverIndex(756);
        WarButton->setLibrary(Libraries::Title);
        WarButton->setHint("TOP 20 Warriors");
        Point tempVar5(60, 38);
        WarButton->setLocation(&tempVar5);
        WarButton->setParent(this);
        WarButton->setSound(SoundList::ButtonA);
        WarButton->Click += [&] (o, e)
        {
            RequestRanks(1);
        };
        WizButton = new MirButton();
        WizButton->setIndex(763);
        WizButton->setPressedIndex(764);
        WizButton->setHoverIndex(765);
        WizButton->setLibrary(Libraries::Title);
        WizButton->setHint("TOP 20 Wizards");
        Point tempVar6(80, 38);
        WizButton->setLocation(&tempVar6);
        WizButton->setParent(this);
        WizButton->setSound(SoundList::ButtonA);
        WizButton->Click += [&] (o, e)
        {
            RequestRanks(2);
        };
        SinButton = new MirButton();
        SinButton->setIndex(757);
        SinButton->setPressedIndex(758);
        SinButton->setHoverIndex(759);
        SinButton->setLibrary(Libraries::Title);
        SinButton->setHint("TOP 20 Assasins");
        Point tempVar7(100, 38);
        SinButton->setLocation(&tempVar7);
        SinButton->setParent(this);
        SinButton->setSound(SoundList::ButtonA);
        SinButton->Click += [&] (o, e)
        {
            RequestRanks(4);
        };
        ArchButton = new MirButton();
        ArchButton->setIndex(766);
        ArchButton->setPressedIndex(767);
        ArchButton->setHoverIndex(768);
        ArchButton->setLibrary(Libraries::Title);
        ArchButton->setHint("TOP 20 Archers");
        Point tempVar8(120, 38);
        ArchButton->setLocation(&tempVar8);
        ArchButton->setParent(this);
        ArchButton->setSound(SoundList::ButtonA);
        ArchButton->Click += [&] (o, e)
        {
            RequestRanks(5);
        };

        NextButton = new MirButton();
        NextButton->setIndex(207);
        NextButton->setHoverIndex(208);
        NextButton->setPressedIndex(209);
        NextButton->setLibrary(Libraries::Prguse2);
        Point tempVar9(299, 386);
        NextButton->setLocation(&tempVar9);
        NextButton->setParent(this);
        NextButton->setSound(SoundList::ButtonA);
        NextButton->Click += [&] (o, e)
        {
            Move(1);
        };

        PrevButton = new MirButton();
        PrevButton->setIndex(197);
        PrevButton->setHoverIndex(198);
        PrevButton->setPressedIndex(199);
        PrevButton->setLibrary(Libraries::Prguse2);
        Point tempVar10(299, 100);
        PrevButton->setLocation(&tempVar10);
        PrevButton->setParent(this);
        PrevButton->setSound(SoundList::ButtonA);
        PrevButton->Click += [&] (o, e)
        {
            Move(-1);
        };

        MyRank = new MirLabel();
        MyRank->setText("");
        MyRank->setParent(this);
        Font tempVar11(Settings::FontName, 10.0F, FontStyle::Bold);
        MyRank->setFont(&tempVar11);
        MyRank->setForeColour(Color::BurlyWood);
        Point tempVar12(229, 36);
        MyRank->setLocation(&tempVar12);
        Size tempVar13(82,22);
        MyRank->setSize(&tempVar13);
        MyRank->setDrawFormat(TextFormatFlags::HorizontalCenter | TextFormatFlags::VerticalCenter);
        MyRank->Click += [&] (o, e)
        {
            GoToMyRank();
        };


        for (int i = 0; i < Rows.Count(); i++)
        {
            RankingRow *tempVar14 = new RankingRow();
            tempVar14->setParent(this);
            Point tempVar15(32, 98 + i * 15);
            tempVar14->setLocation(&tempVar15);
            Size tempVar16(285,15);
            tempVar14->setSize(&tempVar16);
            Rows[i] = tempVar14;

            delete tempVar14;
        }
        for (int i = 0; i < RankList.size(); i++)
        {
            RankList[i] = std::vector<RankCharacterInfo*>();
        }
    }

    void RankingDialog::Show()
    {
        if (getVisible())
        {
            return;
        }
        setVisible(true);
        RequestRanks(RankType);
    }

    void RankingDialog::Hide()
    {
        if (!getVisible())
        {
            return;
        }
        setVisible(false);
    }

    void RankingDialog::Toggle()
    {
        if (!getVisible())
        {
            Show();
        }
        else
        {
            Hide();
        }
    }

    void RankingDialog::GoToMyRank()
    {

    }

    void RankingDialog::Move(int distance)
    {
        if (distance > 0)
        { //go down
            RowOffset = RowOffset < RankList[RankType].size() - 20 ?++RowOffset : RowOffset;
        }
        else
        { //go up
            RowOffset = RowOffset > 0 ?--RowOffset : RowOffset;
        }
        UpdateRanks();
    }

    void RankingDialog::RequestRanks(unsigned char RankType)
    {
        if (RankType > 6)
        {
            return;
        }
        if ((LastRequest[RankType] != 0) && ((LastRequest[RankType] + 300 * 1000) > CMain::Time))
        {
            SelectRank(RankType);
            return;
        }
        LastRequest[RankType] = CMain::Time;
        ClientPackets::GetRanking *tempVar = new ClientPackets::GetRanking();
        tempVar->RankIndex = RankType;
        MirNetwork::Network::Enqueue(tempVar);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
    }

    void RankingDialog::RecieveRanks(std::vector<RankCharacterInfo*> &Ranking, unsigned char rankType, int MyRank)
    {
        RankList[rankType].clear();
        RankList[rankType] = Ranking;
        Rank[rankType] = MyRank;
        SelectRank(rankType);
    }

    void RankingDialog::SelectRank(unsigned char rankType)
    {
        RankType = rankType;
        for (int i = 0; i < Rows.Count(); i++)
        {
            Rows[i]->Clear();
        }
        RowOffset = 0;
        UpdateRanks();
    }

    void RankingDialog::UpdateRanks()
    {
        for (int i = 0; i < Rows.Count(); i++)
        {
            if (RowOffset + i >= RankList[RankType].size())
            {
                break;
            }
            Rows[i]->Update(RankList[RankType][RowOffset + i], RowOffset + i + 1);
        }
        if (Rank[RankType] == 0)
        {
            MyRank->setText("Not Listed");
        }
        else
        {
            MyRank->setText(StringHelper::formatSimple("Ranked: {0}", Rank[RankType]));
        }
        ;

    }

    RankingDialog::RankingRow::RankingRow()
    {
        setSound(SoundList::ButtonA);
        setBorderColour(Color::Lime);
        setVisible(false);
        Click += [&] (o, e)
        {
            Inspect();
        };

        RankLabel = new MirLabel();
        Point tempVar(0, 0);
        RankLabel->setLocation(&tempVar);
        RankLabel->setAutoSize(true);
        RankLabel->setDrawFormat(TextFormatFlags::HorizontalCenter | TextFormatFlags::VerticalCenter);
        RankLabel->setParent(this);
        RankLabel->setNotControl(true);
        NameLabel = new MirLabel();
        Point tempVar2(55, 0);
        NameLabel->setLocation(&tempVar2);
        NameLabel->setAutoSize(true);
        NameLabel->setDrawFormat(TextFormatFlags::HorizontalCenter | TextFormatFlags::VerticalCenter);
        NameLabel->setParent(this);
        NameLabel->setNotControl(true);

        ClassLabel = new MirLabel();
        Point tempVar3(150, 0);
        ClassLabel->setLocation(&tempVar3);
        ClassLabel->setAutoSize(true);
        ClassLabel->setDrawFormat(TextFormatFlags::HorizontalCenter | TextFormatFlags::VerticalCenter);
        ClassLabel->setParent(this);
        ClassLabel->setNotControl(true);

        LevelLabel = new MirLabel();
        Point tempVar4(220, 0);
        LevelLabel->setLocation(&tempVar4);
        LevelLabel->setAutoSize(true);
        LevelLabel->setDrawFormat(TextFormatFlags::HorizontalCenter | TextFormatFlags::VerticalCenter);
        LevelLabel->setParent(this);
        LevelLabel->setNotControl(true);
    }

    void RankingDialog::RankingRow::Inspect()
    {
        if (CMain::Time <= GameScene::InspectTime)
        {
            return;
        }

        GameScene::InspectTime = CMain::Time + 500;
        InspectDialog::InspectID = static_cast<unsigned int>(Index);
        ClientPackets::Inspect *tempVar = new ClientPackets::Inspect();
        tempVar->ObjectID = static_cast<unsigned int>(Index);
        tempVar->Ranking = true;
        MirNetwork::Network::Enqueue(tempVar);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
    }

    void RankingDialog::RankingRow::Clear()
    {
        setVisible(false);
        NameLabel->setText("");
        RankLabel->setText("");
        LevelLabel->setText("");
        ClassLabel->setText("");
    }

    void RankingDialog::RankingRow::Update(RankCharacterInfo *listing, int RankIndex)
    {
        Listing = listing;
        RankLabel->setText(std::to_string(RankIndex));
        LevelLabel->setText(std::to_string(Listing->level));
//C# TO C++ CONVERTER TODO TASK: There is no C++ equivalent to 'ToString':
        ClassLabel->setText(Listing->Class.ToString());
        NameLabel->setText(listing->Name);
        Index = listing->PlayerId;
        if (RankLabel->getText() == "1")
        {
            RankLabel->setForeColour(Color::Gold);
            NameLabel->setForeColour(Color::Gold);
            LevelLabel->setForeColour(Color::Gold);
            ClassLabel->setForeColour(Color::Gold);
        }
        if (RankLabel->getText() == "2")
        {
            RankLabel->setForeColour(Color::Silver);
            NameLabel->setForeColour(Color::Silver);
            LevelLabel->setForeColour(Color::Silver);
            ClassLabel->setForeColour(Color::Silver);
        }
        if (RankLabel->getText() == "3")
        {
            RankLabel->setForeColour(Color::RosyBrown);
            NameLabel->setForeColour(Color::RosyBrown);
            LevelLabel->setForeColour(Color::RosyBrown);
            ClassLabel->setForeColour(Color::RosyBrown);
        }
        else if (NameLabel->getText() == GameScene::getUser()->Name)
        {
            RankLabel->setForeColour(Color::Green);
            NameLabel->setForeColour(Color::Green);
            LevelLabel->setForeColour(Color::Green);
            ClassLabel->setForeColour(Color::Green);
        }
        else if (std::stoi(RankLabel->getText()) > 3)
        {
            RankLabel->setForeColour(Color::White);
            NameLabel->setForeColour(Color::White);
            LevelLabel->setForeColour(Color::White);
            ClassLabel->setForeColour(Color::White);
        }

        setVisible(true);

    }
}
